<?php
/******************************************************************************
 * Konfigurationsseite zum Erstellen eigener Statistiken
 *
 * Copyright    : (c) 2004 - 2012 The Admidio Team
 * Homepage     : http://www.admidio.org
 *
 * License      : GNU Public License 2 http://www.gnu.org/licenses/gpl-2.0.htm
 *
 * Parameters:
 *
 * sta_id       :   (optional) Statistik deren Konfiguration direkt angezeigt werden soll,
 *                  falls nicht angegeben, wird eine neue leere statistik angezeigt.
 *              1 : temporäreDefinition
 * show_as      :   (optional) obwohl die Konfiguration aus der via 'sta_id' ausgelesenen Daten geladen wird,
 *                  wird diese Konfigruation als andere Konfiguration angezeigt und identifiziert.
 *                  Dies ermöglicht eine temporär erzeugte Konfiguration in eine andere bestehende Konfiguration zu speichern.
 *
 *****************************************************************************/

//Import benötigter Skripts
require_once('../includes.php');
require_once(SERVER_PATH.'/adm_program/system/classes/form_elements.php');
require_once(STATISTICS_PATH.'/utils/db_access.php');
require_once(STATISTICS_PATH.'/statistic_objects/statistic.php');

//DB-Hilfsklasse instanzieren
$staDBHandler = new DBAccess();

//Überprüfen, ob das Plugin installiert ist
$pluginInstalled = $staDBHandler->getPluginInstalled();

//Überprüfen, ob der Benutzer Zugriff auf die Seite hat
$hasAccess = false;
foreach ($plgAllowConfig AS $i)
{
    if($i == 'Benutzer'
        && $gValidLogin == true)
    {
        $hasAccess = true;
    }
    elseif($i == 'Rollenverwalter'
        && $gCurrentUser->assignRoles())
    {
        $hasAccess = true;
    }
    elseif($i == 'Listenberechtigte'
        && $gCurrentUser->viewAllLists())
    {
        $hasAccess = true;
    }
    elseif(hasRole($i))
    {
        $hasAccess = true;
    }
}

if ($pluginInstalled) {
    if($hasAccess == true) {
        global $gCurrentOrganization;

        //Erzeugen von Skriptvariablen
        $getStatisticID        = 1;



        $actualProfileFieldsWithSelect [0] = 'Auswahl';
        $actualProfileFieldsWithNo [0] = 'Alle';
        //Aktuell verfügbare Profilfelder auslesen.
        foreach($gProfileFields->mProfileFields as $field)
        {
            $actualProfileFields[$field->getValue('usf_id')] = $field->getValue('usf_name');
            $actualProfileFieldsWithSelect[$field->getValue('usf_id')] = $field->getValue('usf_name');
            $actualProfileFieldsWithNo[$field->getValue('usf_id')] = $field->getValue('usf_name');
        }
        $actualFunctions = array('#'=>'#','%'=>'%','min'=>'min','max'=>'max','avg'=>'avg','sum'=>'sum');
        $actualFunctionsTotal = array(''=>'keine','min'=>'min','max'=>'max','avg'=>'avg','sum'=>'sum');


        //IDs und Namen vorhandener Statistik-Konfigurationen aus der DB holen
        $allStatisticConfigIDs = $staDBHandler->getStatisticIDs($gCurrentOrganization->getValue('org_id',''));
        $allStatisticConfigurations[1] = 'eine neue Statistik-Konfiguration erstellen';
        foreach ($allStatisticConfigIDs as $statisticID){
            $allStatisticConfigurations[$statisticID] = $staDBHandler->getStatisticName($statisticID);
        }

        //Auslesen der Statistik-Konfiguration anhand des Übergabeparameter (falls gesetzt);

        $getScrollPos = admFuncVariableIsValid($_GET, 'scroll_pos', 'numeric', 0);

        if (isset($_GET['sta_id'])){ //falls Statistik-ID gesetzt, vorhandene Konfiguration auslesen
            $getStatisticID      = admFuncVariableIsValid($_GET, 'sta_id', 'numeric', 1);
        }else{
            header('Location: editor_process.php?mode=load&scroll_pos='.$getScrollPos);
        }

        //Es wird immer die temporäre Statistik-Konfiguration aus der DB geholt.
        $statistic = $staDBHandler->getStatistic(1);

        //Arrays für die Beschriftung des Eingabe-Dialoges und die Bezeichnungen der Felder erzeugen.
        $formColumnLabels = array('Spalten','Bezeichnung','Auswahl','Bedingung','Auswerten','Funktion','Total-Funktion','');
        $formColumnInputNames = array('first','label','profile_field','condition','func_arg','func_main','func_total','last');
        $formColumnInputSelectValues = array('profile_field'=>$actualProfileFieldsWithNo,'func_arg'=>$actualProfileFieldsWithSelect,'func_main'=>$actualFunctions,'func_total'=>$actualFunctionsTotal);
        $formRowLabels = array("Zeilen","Bezeichnung","Auswahl","Bedingung","");
        $formRowInputNames = array("first","label","profile_field","condition","last");
        $formRowInputSelectValues = array('profile_field'=>$actualProfileFieldsWithNo);


        //Funktionen für den allgemeinen Gebrauch in diesem Skript


        /*function mockGetStatisticName($ID){
            $mockDB             = array('0'=>'eine neue Konfiguration erstellen','12'=>'Altersstatistik','56'=>'Wohnortstatistik');
            return $mockDB[$ID];
        }*/

        function generateClassSuffix($actualElement, $nrOfElements){
            $classSuffix = "";

            if ($actualElement == 0){
                $classSuffix = "_first";
            }elseif($actualElement == 1){
                $classSuffix = "_second";
            }elseif($actualElement == $nrOfElements-1){
                $classSuffix = "_last";
            }

            return $classSuffix;
        }

        function removeScrollPosFromURL($currentURL){
            $pattern = '/&scroll_pos=\d*/';
            $replacement = '';
            $subject = $currentURL;
            return preg_replace($pattern,$replacement,$subject);
        }

        // Url fuer die Zuruecknavigation merken
        $_SESSION['navigation']->addUrl(removeScrollPosFromURL(CURRENT_URL));

        function generateStatisticConfigSelectBox($entryArray, $defaultEntry = '', $fieldId = 'admSelectBox', $optAttributes = '', $createFirstEntry = false)
        {
            global $gL10n;

            $selectBoxHtml = '<select size="1" id="'.$fieldId.'" name="'.$fieldId.'" '.$optAttributes.'>';
            if($createFirstEntry == true)
            {
                $selectBoxHtml .= '<option value=" "';
                if(strlen($defaultEntry) == 0){
                    $selectBoxHtml .= ' selected="selected" ';
                }
                $selectBoxHtml .= '>- '.$gL10n->get('SYS_PLEASE_CHOOSE').' -</option>';
            }

            $value = reset($entryArray);
            for($arrayCount = 0; $arrayCount < count($entryArray); $arrayCount++)
            {
                // create entry in html
                $selectBoxHtml .= '<option value="'.key($entryArray).'"';
                if(key($entryArray) == $defaultEntry)
                {
                    $selectBoxHtml .= ' selected="selected" ';
                }
                $selectBoxHtml .= '>'.$value.'</option>';
                $value = next($entryArray);
            }
            $selectBoxHtml .= '</select>';
            return $selectBoxHtml;
        }

        $stdFrameStyle = 'border:1px solid #7d7d7d; margin: 10px 0px;padding: 10px; overflow: auto;';

        $gLayout['title']  = 'Statistik Konfigurieren';
        $gLayout['header'] = '

            <style type="text/css">

                #form_sta_config {
        /*            border:     1px solid #7d7d7d;
                    padding-left: 10px;
                    padding-right: 10px;*/
                }

                .stdDiv {
                    border:1px solid #7d7d7d;
                    margin: 10px 0px;
                    padding: 10px;
                    overflow-x: auto;
                }
        		
        		.helpRight{
        			float: right;
    			}

                .InputLabelBox{
                    height: 25px;
                    margin-top: 10px;
                }

                .textLabel{
                    margin-top: 3px;
                    float: left;
                }

                .textInput{
                    float: right;
                    width: 280px;
                }

                .roleInput{
                    float: right;
                    width: 285px;
                }
                #statistic_std_role{
                    float: right;
                    width: 285px;
                }

        /*Regeln für die Tabelle mit den Spaltendefinitionen*/
                .col_conf_tbl{
                    /*background-color: #adff2f;
                    border:2px solid #7d7d7d;

                    border-collapse: collapse;*/
                }

                .col_conf_tbl_col_first_first{
                    min-width: 120px;
                    color: #449ba6;
                    font-size: 110%;
                }

                .col_conf_tbl_col_last_first{
                    font-size: 90%;
                }

                .col_conf_tbl_row_first{
                    font-weight: bold;
                }

                .col_conf_tbl_row_label{
                    color: orange;
                    font-weight: bold;
                }

                .col_conf_tbl_col_label, .col_conf_tbl_col_label_second, .col_conf_tbl_col_label_last {
                    border:2px solid orange;
                }

                .col_conf_tbl_row_profile_field{
                    color: #add8e6;
                    font-weight: bold;
                }

                .col_conf_tbl_col_profile_field, .col_conf_tbl_col_profile_field_second, .col_conf_tbl_col_profile_field_last {
                    border:2px solid #add8e6;
                }

                .col_conf_tbl_row_condition{
                    color: #add8e6;
                    font-weight: bold;
                }

                .col_conf_tbl_col_condition, .col_conf_tbl_col_condition_second, .col_conf_tbl_col_condition_last {
                    border:2px solid #add8e6;
                }
                .col_conf_tbl_row_func_arg{
                    color: #9acd32;
                    font-weight: bold;
                }
                .col_conf_tbl_col_func_arg, .col_conf_tbl_col_func_arg_second, .col_conf_tbl_col_func_arg_last {
                    border:2px solid #9acd32;
                }
                .col_conf_tbl_row_func_main{
                    color: #9acd32;
                    font-weight: bold;
                }
                .col_conf_tbl_col_func_main, .col_conf_tbl_col_func_main_second, .col_conf_tbl_col_func_main_last {
                    border:2px solid #9acd32;
                }
                .col_conf_tbl_row_func_total{
                    color: #fa8072;
                    font-weight: bold;
                }
                .col_conf_tbl_col_func_total, .col_conf_tbl_col_func_total_second, .col_conf_tbl_col_func_total_last {
                    border:2px solid #fa8072;
                }

                .col_conf_tbl select{
                    width: 120px;
                }

                .col_conf_tbl input{
                    width: 115px;
                }


        /*Regeln für die Tabelle mit den Zeilendefinitionen*/
                .row_conf_tbl{
                    /*background-color: #40e0d0;
                    border:1px solid #7d7d7d;
                    border-collapse: collapse;*/
                }

        /*        .row_conf_tbl_row{
                    font-style: italic;
                }*/

                .row_conf_tbl_col_last{
                    background-color: inherit;
                }

                .row_conf_tbl_col_label, .row_conf_tbl_col_label_second{
                    border:2px solid orange;
                }

                .row_conf_tbl_col_first_first {
                    min-width: 120px;
                    color: #449ba6;
                    font-size: 110%;
                }
                .row_conf_tbl_col_first_last {
                    font-size: 90%;
                }

                .row_conf_tbl_col_label_first{
                    color: orange;
                    font-weight: bold;
                }
                .row_conf_tbl_col_profile_field{
                    border:2px solid #87cefa;
                }
                .row_conf_tbl_col_profile_field_first{
                    color: #87cefa;
                    font-weight: bold;
                }
                .row_conf_tbl_col_condition{
                     border:2px solid  #87cefa;
                }
                .row_conf_tbl_col_condition_first{
                    color: #87cefa;
                    font-weight: bold;
                }

                .row_conf_tbl_col_first,.row_conf_tbl_col_first_first, .row_conf_tbl_col_first_second{
                    font-weight: bold;
                }


                .row_conf_tbl_row_first  {

                }

                .row_conf_tbl_row_last  {

                }

                .row_conf_tbl select{
                    width: 120px;
                }

                .row_conf_tbl input{
                    width: 115px;
                }


            </style>

            <script type="text/javascript">
                function loadConf(newConf){
                    var sta_id = $("#statistic_conf_select").attr("value");
                    if (sta_id == 1 || newConf == true){
                        self.location.href = "editor_process.php?mode=load"+saveScrollPos();
                    }else{
                        self.location.href = "editor_process.php?mode=load&sta_id="+sta_id+saveScrollPos();
                    }
                }

                function deleteConfiguration(){
                    var sta_id = $("#statistic_conf_select").attr("value");
                    if (sta_id == 1){
                        alert("Bitte wähle eine Konfiguration aus, die du löschen möchtest.");
                    }else{
                        var select_box = document.getElementById("statistic_conf_select");
                        var current_text = select_box.options[select_box.selectedIndex].text;
                        Check = confirm("Willts du die Konfiguration \'"+current_text+"\' wirklick löschen?");
                        if (Check == true){
                            self.location.href = "editor_process.php?mode=delete&sta_id="+sta_id+saveScrollPos();
                        }
                    }
                }

                function disableConditionInput(idOfElement){
                    var nameOfElement = idOfElement.getAttribute("id");
                    var nameOfNewElement = nameOfElement.replace("profile_field","condition");
                    if (idOfElement.value == 0){
                        document.getElementById(nameOfNewElement).disabled=true;
                    }else{
                        document.getElementById(nameOfNewElement).disabled=false;
                    }
                }

                function checkAllSelectBoxes(){
                    var nrOfSelectBoxes = document.getElementsByTagName("select").length;
                    for (var i = 0; i < nrOfSelectBoxes; i++){
                        var tmpSelectBox = document.getElementsByTagName("select")[i];
                        var tmpSelectBoxId = tmpSelectBox.getAttribute("id");
                        var testString = /(func_arg)/g;
                        var validTest = testString.test(tmpSelectBoxId);
                        if (validTest != false){
                            disableInvalidFunctions(tmpSelectBox);
                        }else{
                            testString = /(profile_field)/g;
                            validTest = testString.test(tmpSelectBoxId);
                             if (validTest != false){
                                disableConditionInput(tmpSelectBox);
                            }
                        }
                    }
                }

                function replaceGTLT(){
                    var nrOfInputBoxes = document.getElementsByTagName("input").length;
                    var i = 0;
                    for (var i = 0; i < nrOfInputBoxes; i++){
                        var tmpInputBox = document.getElementsByTagName("input")[i];
                        var tmpInputBoxId = tmpInputBox.getAttribute("id");
                        var testString = /(condition)/g;
                        var validTest = testString.test(tmpInputBoxId);
                        if (validTest != false){
                            tmpInputBox.value = tmpInputBox.value.replace("<","{");
                            tmpInputBox.value = tmpInputBox.value.replace(">","}");
                        }
                    }

                }

                function adaptStdStatisticRoleSelectBox(){
                    document.form_sta_config.statistic_std_role.options[0] = null;
                }


                function disableInvalidFunctions(idOfElement){
                    var nameOfElement = idOfElement.getAttribute("id");
                    var nameOfNewElement = nameOfElement.replace("func_arg","func_main");
                    var ElementToChange =  document.getElementById(nameOfNewElement);
                    if (idOfElement.value == 0){
                        ElementToChange.options[0].disabled=false;
                        ElementToChange.options[1].disabled=false;
                        ElementToChange.options[2].disabled=true;
                        ElementToChange.options[3].disabled=true;
                        ElementToChange.options[4].disabled=true;
                        ElementToChange.options[5].disabled=true;
                        if (ElementToChange.selectedIndex >= 2 && ElementToChange.selectedIndex <= 5){
                            ElementToChange.selectedIndex = 0;
                        }
                    }else{
                        ElementToChange.options[0].disabled=true;
                        ElementToChange.options[1].disabled=true;
                        ElementToChange.options[2].disabled=false;
                        ElementToChange.options[3].disabled=false;
                        ElementToChange.options[4].disabled=false;
                        ElementToChange.options[5].disabled=false;
                        if (ElementToChange.selectedIndex >= 0 && ElementToChange.selectedIndex <= 1){
                            ElementToChange.selectedIndex = 5;
                        }
                    }
                }

                function saveScrollPos(){
                    var scroll_pos = window.pageYOffset;
                    var return_scroll_pos = "";
                    if (scroll_pos != undefined && scroll_pos != 0){
                        return_scroll_pos = "&scroll_pos="+ scroll_pos;
                    }
                    return return_scroll_pos;
                }

                function editStructure(action, tableNr, colNr, rowNr){
                    var sta_id = $("#statistic_conf_select").attr("value");
                    var submitParameters = "?sta_id="+sta_id+"&mode=editstructure&editaction=" + action;
                    if (tableNr != undefined && tableNr !=""){
                        submitParameters += "&edittblnr="+ tableNr;
                    }
                    if (colNr != undefined && colNr !=""){
                        submitParameters += "&editcolnr="+ colNr;
                    }
                    if (rowNr != undefined && rowNr !=""){
                        submitParameters += "&editrownr="+ rowNr;
                    }
                    submitParameters += saveScrollPos();
                    document.form_sta_config.action = document.form_sta_config.action + submitParameters;
                    replaceGTLT();
                    document.form_sta_config.submit();
                }

                function doFormSubmit(mode){
                    var doSubmit = false;
                    var sta_id = $("#statistic_conf_select").attr("value");
                    var default_prompttext = "Neue Konfiguration";
                    if (mode == "saveas"){
                        mode = "save";
                        sta_id = 1;
                        var select_box = document.getElementById("statistic_conf_select");
                        default_prompttext = select_box.options[select_box.selectedIndex].text+" - Kopie";
                    }

                    var submitParameters = "?mode=" + mode;

                    if (sta_id == 1 && mode == "save"){
                        name = prompt("Bitte gib einen Namen für die neue Konfiguration an:",default_prompttext);
                        if (name == undefined || name == ""){
                            alert("Du musst einen Namen für die neue Konfiguration angeben!");
                        }else{
                            submitParameters += "&name="+ name;
                            doSubmit = true;
                        }
                    }else{
                        submitParameters += "&sta_id=" + sta_id;
                        doSubmit = true;
                    }

                    if (doSubmit){
                        submitParameters += saveScrollPos();
                        document.form_sta_config.action = document.form_sta_config.action + submitParameters;
                        replaceGTLT();
                        document.form_sta_config.submit();
                    }
                }

            </script>';
        $doOnLoad = 'checkAllSelectBoxes(); adaptStdStatisticRoleSelectBox();';
        if ($getScrollPos != 0){
            $doOnLoad .= ' scrollTo(0,'.$getScrollPos.');';
        }
        $gLayout['onload'] = $doOnLoad;


        require_once(SERVER_PATH. '/adm_program/system/overall_header.php');

        $tables = $statistic->getTables();
        $nrOfTables = count($tables);
        echo '
         <div class="formHead">Statistik-Editor</div>
        <form id="form_sta_config" name="form_sta_config" action="editor_process.php" method="post">

            <div class= "stdDiv" id="div_config_selection">

                <p>Möchtest du eine gespeicherte Konfiguration laden und verändern oder eine neue Statistikkonfiguration erstellen?</p>'
                .generateStatisticConfigSelectBox($allStatisticConfigurations,$getStatisticID,'statistic_conf_select','onchange="loadConf()"',false).'
                <a href="javascript: doFormSubmit(\'save\')"><img
                        src="'. THEME_PATH. '/icons/disk.png" title="Konfiguration speichern" alt="Konfiguration speichern"/></a>
                <a href="javascript: doFormSubmit(\'saveas\')"><img
                        src="'. THEME_PATH. '/icons/disk_copy.png" title="Konfiguration speichern unter" alt="Konfiguration speichern unter"/></a>
                <a href="javascript: loadConf(true)"><img
                        src="'. THEME_PATH. '/icons/add.png" title="neue Konfiguration erstellen" alt="neue Konfiguration erstellen"/></a>
                <a href="javascript: loadConf()"><img
                        src="'. THEME_PATH. '/icons/arrow_turn_left.png" title="alle Änderungen an der aktuellen Konfiguration rückgängig machen" alt="rückgängig"/></a>
                <a href="javascript: deleteConfiguration()"><img
                        src="'. THEME_PATH. '/icons/delete.png" title="Konfiguration löschen" alt="Konfiguration löschen"/></a>
                <a href="../resources/Benutzerhandbuch.pdf"><img
                        class="iconHelpLink" src="'. THEME_PATH. '/icons/help.png" alt="Help" title="Handbuch des Statistik-Plugins öffnen" /></a>

                <h3>Allgemeine Angaben zur Statistik</h3>
                <div class ="InputLabelBox">
                    <span class="textLabel">Titel der Statistik</span>
                    <input class ="textInput" type="text" name="statistic_title" id="statistic_title" value="'.$statistic->getTitle().'">
                </div>
                <div class ="InputLabelBox">
                    <span class="textLabel">Untertitel der Statistik</span>
                    <input class ="textInput" type="text" name="statistic_subtitle" id="statistic_subtitle" value="'.$statistic->getSubtitle().'">
                </div>
                <div class ="InputLabelBox">
                    <span class="textLabel">Standardrolle der Statistik</span>
                    <a rel="colorboxHelp" href="help.php?help_id=533">
                        <img class="iconHelpLink" src="'. THEME_PATH. '/icons/help.png" alt="Help" title="Hilfe zur Standardrolle anzeigen" />
                    </a>'
                    .FormElements::generateRoleSelectBox($statistic->getStandardRoleID(),'statistic_std_role').'
                    <input class ="textInput" type="hidden" name="nr_of_tables" id="nr_of_tables" value="'.$nrOfTables.'">
                </div>

            </div>
        ';

        //Schleife für die Erzegung der Eingabemaske für die Tabellen-Konfigurationen
        for ($tc=0;$tc<$nrOfTables;$tc++) {
            $columns = $tables[$tc]->getColumns();
            $rows = $tables[$tc]->getRows();
            $effectiveNrOfColumns = count($columns);
            $effectiveNrOfRows = count($rows);
            $nrOfColumns = $effectiveNrOfColumns+1;
            $nrOfRows =$effectiveNrOfRows+3;
            echo '
            <div class= "stdDiv" id="div_table'.$tc.'_config">
                <h3>'.($tc+1).'. Tabelle</h3>
                <div class ="InputLabelBox">
                    <span class="textLabel">Titel der Tabelle</span>
                    <input class ="textInput" type="text" name="table'.$tc.'_title" id="table'.$tc.'_title" value="'.$tables[$tc]->getTitle().'">
                </div>
                <div class ="InputLabelBox">
                    <span class="textLabel">Rolle der Tabelle</span>
                    <a rel="colorboxHelp" href="help.php?help_id=542">
                        <img class="iconHelpLink" src="'. THEME_PATH. '/icons/help.png" alt="Help" title="Hilfe zur Rolle der Tabelle anzeigen" />
                    </a>'
                    .FormElements::generateRoleSelectBox($tables[$tc]->getRoleID(),'table'.$tc.'_role" class="roleInput').'
                </div>
                <input type="hidden" name="table'.$tc.'_nr_of_columns" id="table'.$tc.'_nr_of_columns" value="'.$effectiveNrOfColumns.'">
                <input type="hidden" name="table'.$tc.'_nr_of_rows" id="table'.$tc.'_nr_of_rows" value="'.$effectiveNrOfRows.'">
                <div class= "stdDiv" id="div_table'.$tc.'_column_config">

                    <table class="col_conf_tbl">
            ';

            //Schleife für die Erzegung der Eingabe-Tabelle der Spalten-Konfigurationen
            for ($frmRow=0;$frmRow<8;$frmRow++) {
                if ($frmRow == 0){
                    echo '<thead>';
                } elseif ($frmRow == 1){
                    echo '<tbody>';
                }

                echo '<tr class="col_conf_tbl_row_'.$formColumnInputNames[$frmRow].'">';
                for ($cc=0;$cc<$nrOfColumns;$cc++) {
                    $colIdf = $cc-1;
                    $classSuffix = generateClassSuffix($cc,$nrOfColumns);
                    echo '<td class="col_conf_tbl_col_'.$formColumnInputNames[$frmRow].$classSuffix.'">';
                    if ($cc == 0){
                        if ($frmRow == 7){
                            echo'<a href="javascript: editStructure(\'addcol\',\''.$tc.'\')"><img src="'. THEME_PATH. '/icons/add.png" alt="Spalte hinzufügen"/>Spalte hinzufügen</a>';
                        }else{
                            echo $formColumnLabels[$frmRow];
                        }

                        if ($frmRow > 0 && $frmRow < 7){
                            echo '
                            <a rel="colorboxHelp" href="help.php?help_id=55'.$frmRow.'">
                                <img class="iconHelpLink helpRight" src="'. THEME_PATH. '/icons/help.png" alt="Help" title="Hilfe zu diesem Thema anzeigen" />
                            </a>';
                        }

                    }else{
                        if ($frmRow == 0){
                            echo ($cc).'. Spalte';
                        }elseif ($frmRow == 1){
                            echo '<input name="table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow].'" id="table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow].'" type="text" value="'.$columns[$colIdf]->getLabel().'">';
                        }elseif ($frmRow == 2){
                            echo FormElements::generateDynamicSelectBox($formColumnInputSelectValues[$formColumnInputNames[$frmRow]],$columns[$colIdf]->getCondition()->getProfileFieldID(),'table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow].'" onchange="disableConditionInput(this)',false);
                        }elseif ($frmRow == 3){
                            echo '<input name="table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow].'" id="table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow].'" type="text" value="'.$columns[$colIdf]->getCondition()->getUserCondition().'">';
                        }elseif ($frmRow == 4){
                            echo FormElements::generateDynamicSelectBox($formColumnInputSelectValues[$formColumnInputNames[$frmRow]],$columns[$colIdf]->getFunction()->getArgument(),'table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow].'" onchange="disableInvalidFunctions(this)',false);
                        }elseif ($frmRow == 5){
                            echo FormElements::generateDynamicSelectBox($formColumnInputSelectValues[$formColumnInputNames[$frmRow]],$columns[$colIdf]->getFunction()->getName(),'table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow],false);
                        }elseif ($frmRow == 6){
                            echo FormElements::generateDynamicSelectBox($formColumnInputSelectValues[$formColumnInputNames[$frmRow]],$columns[$colIdf]->getFunctionTotal(),'table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow],false);
                        }elseif ($frmRow == 7){
                            if ($effectiveNrOfColumns > 1){
                                echo '<a href="javascript: editStructure(\'delcol\',\''.$tc.'\',\''.$colIdf.'\')"><img src="'. THEME_PATH. '/icons/delete.png" alt="Spalte löschen"/></a>';
                            }
                        }else{
                            echo '<input name="table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow].'" id="table'.$tc.'_column'.$colIdf.'_'.$formColumnInputNames[$frmRow].'" type="text">';
                        }
                    }
                    echo '</td>';
                }
                echo '</tr>';

                if ($frmRow == 0){
                    echo '</thead>';
                } elseif ($frmRow == 7){
                    echo '</tbody>';
                }
            }

            echo '
                </table>
            </div>
            <div class= "stdDiv" id="div_table'.$tc.'_row_config">
                <table class="row_conf_tbl">';

            //Schleife für die Erzegung der Eingabe-Tabelle der Zeilen-Konfigurationen
            for ($rc=0;$rc<$nrOfRows;$rc++) {
                $rowIdf = $rc-2;
                $classSuffix = generateClassSuffix($rc,$nrOfRows);
                if ($rc == 0){
                    echo '<thead>';
                } elseif ($rc == 1){
                    echo '<tbody>';
                }
                echo '<tr class="row_conf_tbl_row'.$classSuffix.'">';
                for ($frmCol=0;$frmCol<5;$frmCol++) {
                    echo '<td class="row_conf_tbl_col_'.$formRowInputNames[$frmCol].$classSuffix.'">';
                    if ($rc == 0){
                        echo $formRowLabels[$frmCol];
                        if ($frmCol > 0 && $frmCol < 4){
                            echo '
                            <a rel="colorboxHelp" href="help.php?help_id=56'.($frmCol+1).'">
                                <img class="iconHelpLink" src="'. THEME_PATH. '/icons/help.png" alt="Help" title="Hilfe zu diesem Thema anzeigen" />
                            </a>';
                        }
                    }elseif($rc == 1){
                        if ($frmCol == 0){
                            echo 'Kopfzeile';
                        }elseif ($frmCol == 1){
                            echo '<input name="table'.$tc.'_first_column_label" id="table'.$tc.'_first_column_label" type="text" value="'.$tables[$tc]->getFirstColumnLabel().'">';
                        }
                    }elseif ($rc == $nrOfRows-1){
                        if ($frmCol == 0){

                        }
                    }else{
                        if ($frmCol == 0){
                            echo ($rc-1).'. Zeile';
                        }elseif($frmCol == 1){
                            echo '<input name="table'.$tc.'_row'.$rowIdf.'_'.$formRowInputNames[$frmCol].'" id="table'.$tc.'_row'.$rowIdf.'_'.$formRowInputNames[$frmCol].'" type="text" value="'.$rows[$rowIdf]->getLabel().'">';
                        }elseif($frmCol == 2){
                            echo FormElements::generateDynamicSelectBox($formRowInputSelectValues[$formRowInputNames[$frmCol]],$rows[$rowIdf]->getCondition()->getProfileFieldID(),'table'.$tc.'_row'.$rowIdf.'_'.$formRowInputNames[$frmCol].'" onchange="disableConditionInput(this)',false);
                        }elseif($frmCol == 3){
                            echo '<input name="table'.$tc.'_row'.$rowIdf.'_'.$formRowInputNames[$frmCol].'" id="table'.$tc.'_row'.$rowIdf.'_'.$formRowInputNames[$frmCol].'" type="text" value="'.$rows[$rowIdf]->getCondition()->getUserCondition().'">';
                        }elseif($frmCol == 4 && $effectiveNrOfRows > 1){
                            echo '<a href="javascript: editStructure(\'delrow\',\''.$tc.'\',\'\',\''.$rowIdf.'\')"><img src="'. THEME_PATH. '/icons/delete.png" alt="Zeile löschen"/></a>';
                        }
                    }
                    echo '</td>';
                }
                echo '</tr>';

                if ($rc == 0){
                    echo '</thead>';
                } elseif ($rc == $nrOfRows-1){
                    echo '</tbody>';
                }

            }
            echo '</table>';
            echo '<span class="iconTextLink"><a href="javascript: editStructure(\'addrow\',\''.$tc.'\')"><img src="'. THEME_PATH. '/icons/add.png" alt="Zeile hinzufügen"/></a>';
            echo '<a href="javascript: editStructure(\'addrow\',\''.$tc.'\')">Zeile hinzufügen</a></span>';
            echo   '</div>';

                if ($nrOfTables >1){
                    echo '<a href="javascript: editStructure(\'deltable\',\''.$tc.'\')"><img src="'. THEME_PATH. '/icons/delete.png" alt="Tabelle löschen"/>Tabelle löschen</a>
                    ';
                }
            echo '</div>';
        }
        echo    '<span class="iconTextLink"><a href="javascript: editStructure(\'addtable\')"><img src="'. THEME_PATH. '/icons/add.png" alt="Tabelle hinzufügen"/></a>';
        echo    '<a href="javascript: editStructure(\'addtable\')">Tabelle hinzufügen</a></span>';
        echo    '<a rel="colorboxHelp" href="help.php?help_id=428">
                    <img class="iconHelpLink" src="'. THEME_PATH. '/icons/help.png" alt="Help" title="Hilfe zum Hinzufügen von Tabellen anzeigen" />
                 </a>';
        echo    '<br /><br /><input type="button" name="show_statistic" value="Statistik anzeigen" onclick="javascript: doFormSubmit(\'show\')">';
        echo    '<a rel="colorboxHelp" href="help.php?help_id=427">
                    <img class="iconHelpLink" src="'. THEME_PATH. '/icons/help.png" alt="Help" title="Hilfe zur Statistik-Vorschau anzeigen" />
                 </a>';
        echo '</form>';
    } else  {

        require_once(SERVER_PATH. '/adm_program/system/overall_header.php');

        if ($gValidLogin) {
            echo '<p>Sie haben keine Berechtigung, diese Seite anzuzeigen.</p>';
        } else {
            require_once(SERVER_PATH.'/adm_program/system/login_valid.php');
        }
    }
} else {

    require_once(SERVER_PATH. '/adm_program/system/overall_header.php');

    echo '<p>Das Plugin ist nicht installiert, bitte zuerst installieren.</p>';
    $text = 'Zur Installation';
    $link = '../install/install.php';
    echo '<p><form action="'. $link . '" method="post"  >
                <input type="submit" name="action" value="' . $text . '" />
            </form>
           </p>';

}

// Url fuer die Zuruecknavigation merken, ohne Scrollposition zu entfernen!
$_SESSION['navigation']->addUrl(CURRENT_URL);

echo '
        <ul class="iconTextLinkList">
            <li>
                <span class="iconTextLink">
                <span class="iconTextLink">
                    <a href="'.$g_root_path.'/adm_program/system/back.php"><img
                    src="'. THEME_PATH. '/icons/back.png" alt="'.$gL10n->get('SYS_BACK').'" /></a>
                    <a href="'.$g_root_path.'/adm_program/system/back.php">'.$gL10n->get('SYS_BACK').'</a>
                </span>
            </li>
        </ul>';

require(SERVER_PATH. '/adm_program/system/overall_footer.php');
?>
